﻿using System;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Diagnostics;


namespace MyVoiceAssistant
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer ss = new SpeechSynthesizer();
        SpeechRecognitionEngine sr = new SpeechRecognitionEngine();
        PromptBuilder pb = new PromptBuilder();
        Choices clist=new Choices();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            button2.Enabled = true;
            clist.Add(new string[]{"hello","time please", "open fb", "open google" });
            //you can add as muxh as words here as grammar
            Grammar gr = new Grammar(new GrammarBuilder(clist));

            try
            {
                sr.RequestRecognizerUpdate();
                sr.LoadGrammar(gr);
                sr.SpeechRecognized += Sr_SpeechRecognized;
                sr.SetInputToDefaultAudioDevice();
                sr.RecognizeAsync(RecognizeMode.Multiple);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "error");
            }
        }

        private void Sr_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            switch (e.Result.Text.ToString())
            {
                case "hello":
                    ss.SpeakAsync("hello there");
                    break;
                case "time please":
                    ss.SpeakAsync("The time is " + DateTime.Now.ToLongTimeString());
                    break;
                case "open fb":
                    Process.Start("chrome", "http://facebook.com");
                    break;
                case "open google":
                    Process.Start("chrome", "http://google.com");
                    break;
                    //create case statement for each words you added
                default:
                    ss.SpeakAsync("sorry");
                    break;

            }
            textBox1.Text += e.Result.Text.ToString() + Environment.NewLine;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sr.RecognizeAsyncStop();
            button1.Enabled = true;
            button2.Enabled = false;
        }
    }
}
